"""
import pygame
import os
import random
import time

# PyGame详解 http://m.biancheng.net/pygame/time.html
ROW=3
COLUMN=4
B=150
W=B*COLUMN
H=B*(ROW+1)

EXT=None
EXT_X=B*3
EXT_Y=B*3

random.seed(time.time_ns())

def splitImage(bg_img:pygame.Surface):
    w=bg_img.get_width()
    h=bg_img.get_height()
    imgs=[]
    for j in range(ROW):
        sub_imgs=[]
        for i in range(COLUMN):
            sm_img=pygame.Surface((B,B))
            sm_img.blit(bg_img,(-(i*B),-(j*B)))
            sub_imgs.append(sm_img)
        imgs.append(sub_imgs)
    return imgs

def getBlock(bs,x,y):
    if x>=0 and x<COLUMN and y>=0 and y<ROW:
        return bs[y][x]
    return None

def drawAllBlock(screen,bs):
    pygame.draw.rect(screen,[255,255,255],[0,0,W,B*3],0)
    for j in range(ROW):
        for i in range(COLUMN):
            b=getBlock(bs,i,j)
            if b:
                screen.blit(b,(i*B,j*B))
    if EXT:
        screen.blit(EXT,(EXT_X,EXT_Y))
    else:
        pygame.draw.rect(screen,[255,255,255],[EXT_X,EXT_Y,B,B],0)
    # 绘制棋盘格
    pygame.draw.line(screen,[0,0,0],(0,0),(W,0))
    for i in range(ROW):
        y=(i+1)*B
        pygame.draw.line(screen,[0,0,0],(0,y),(W,y))
    pygame.draw.line(screen,[0,0,0],(0,0),(0,B*3))
    for i in range(COLUMN):
        x=(i+1)*B
        pygame.draw.line(screen,[0,0,0],(x,0),(x,B*3))
    # 绘制扩展区域
    pygame.draw.rect(screen,(0,0,0),(EXT_X,EXT_Y,EXT_X+B,EXT_Y+B),1)

def canMoveBlock(bs,x,y):
    global EXT
    b=getBlock(bs,x,y)
    if not b:
        if x==3 and y==3 and EXT!=None:
            if y-1>=0 and getBlock(bs,x,y-1)==None:
                #可以移动 向上
                return True
        return False
    if EXT==None and x+1==COLUMN and y+1==ROW:
        #可以移动 右下方向下到扩展块
        return True
    else:
        if y-1>=0 and getBlock(bs,x,y-1)==None:
            #可以移动 向上
            return True
        elif x+1<COLUMN and getBlock(bs,x+1,y)==None:
            #可以移动 向右
            return True
        elif y+1<ROW and getBlock(bs,x,y+1)==None:
            #可以移动 向下
            return True
        elif x-1>=0 and getBlock(bs,x-1,y)==None:
            #可以移动 向左
            return True
    return False
# 移动图片
# bs为全部小图片，x,y为要移动的块的坐标，移动方向自动判断
# 并返回是否移动成功
def moveBlock(bs,x,y):
    print(f"move {x} {y}")
    global EXT
    b=getBlock(bs,x,y)
    if not b:
        if x==3 and y==3 and EXT!=None:
            if y-1>=0 and getBlock(bs,x,y-1)==None:
                #可以移动 向上
                print(f"move to up")
                bs[y-1][x]=EXT
                EXT=None
                return True
        return False
    if EXT==None and x+1==COLUMN and y+1==ROW:
        #可以移动 右下方向下到扩展块
        print(f"move to ext")
        EXT=b
        bs[y][x]=None
        return True
    else:
        if y-1>=0 and getBlock(bs,x,y-1)==None:
            #可以移动 向上
            print(f"move to up")
            bs[y-1][x]=b
            bs[y][x]=None
            return True
        elif x+1<COLUMN and getBlock(bs,x+1,y)==None:
            #可以移动 向右
            print(f"move to right")
            bs[y][x+1]=b
            bs[y][x]=None
            return True
        elif y+1<ROW and getBlock(bs,x,y+1)==None:
            #可以移动 向下
            print(f"move to down")
            bs[y+1][x]=b
            bs[y][x]=None
            return True
        elif x-1>=0 and getBlock(bs,x-1,y)==None:
            #可以移动 向左
            print(f"move to left")
            bs[y][x-1]=b
            bs[y][x]=None
            return True
    return False

# 打乱
def shuff(bs):
    x,y=3,3 # 这里记录的是空格位置
    i=0
    while i<150:
        i+=1
        can_move=[]
        if canMoveBlock(bs,x,y-1):
            can_move.append("上")
        if canMoveBlock(bs,x,y+1):
            can_move.append("下")
        if canMoveBlock(bs,x-1,y):
            can_move.append("左")
        if canMoveBlock(bs,x+1,y):
            can_move.append("右")
        r=random.randint(0,len(can_move)-1)
        r=can_move[r]
        print(f"{i} curr x={x}, y={y} can move={len(can_move)} r={r}")
        if r=="上":
            if moveBlock(bs,x,y-1):
                y-=1
        elif r=="右":
            if moveBlock(bs,x+1,y):
                x+=1
        elif r=="下":
            if moveBlock(bs,x,y+1):
                y+=1
        elif r=="左":
            if moveBlock(bs,x-1,y):
                x-=1


def main():
    pygame.init()
    pygame.display.set_caption("拼图游戏")
    img_wh=img_w,img_h=W,B*ROW
    sc_wh=screen_w,screen_h=W,H
    screen=pygame.display.set_mode((screen_w,screen_h))

    clock=pygame.time.Clock()

    bg_img=pygame.image.load("./python3/study/03.pintu/bg01.jpg").convert()
    bg_img=pygame.transform.scale(bg_img,img_wh)
    sm_img=pygame.transform.scale(bg_img,(B/2*4/3,B/2))
    print(bg_img)
    screen.fill((255,255,255))

    screen.blit(sm_img,(((3*B)-(B*2/3))/2,B*3+B/4))
    pygame.display.update
    bs=splitImage(bg_img)
    shuff(bs)
    run=True
    while run:
        clock.tick(20)
        drawAllBlock(screen,bs)
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                run=False
            elif event.type == pygame.MOUSEBUTTONUP:
                print(event.pos)
                if event.button==1:
                    x,y=event.pos
                    i=x//B
                    j=y//B
                    if moveBlock(bs,i,j):
                        drawAllBlock(screen,bs)
        pygame.display.update()
    pygame.quit()

main()
"""
import pygame,os,random,time
def shua():
    pygame.display.flip()
def abc(a,b,c,B):
    pygame.draw.rect(screen,(a,0,0),(0,71*B,34*B,30*B),0) 
    pygame.draw.rect(screen,(0,b,0),(34*B,71*B,33*B,30*B),0) 
    pygame.draw.rect(screen,(0,0,c),(67*B,71*B,33*B,30*B),0)
    pygame.draw.rect(screen,(a,b,c),(32*B,71*B,3*B,30*B),0) 
    pygame.draw.rect(screen,(a,b,c),(65*B,71*B,3*B,30*B),0)
gao,chang,B,a,b,c=800,1000,10,250,250,250
pygame.init()
pygame.display.set_caption("拼图游戏")
screen=pygame.display.set_mode([chang,gao])
screen.fill([255,255,255])
abc(a,b,c,B)
shua()
clock=pygame.time.Clock()
while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
        elif event.type == pygame.MOUSEBUTTONUP:
                #print(event.pos)
                if event.button==1:
                    x,y=event.pos
                    x,y=int(x/B),int(y/B)
                #print(x,y)
                if y<71:
                    pygame.draw.rect(screen,(a,b,c),(x*B,y*B,B,B),B)
                else:
                    if x<17 and a>9:
                         a-=10
                    if x>17 and x<34 and a<246:
                         a+=10
                    if x>34 and x<50 and b>9:
                         b-=10
                    if x>50 and x<67 and b<246:
                         b+=10
                    if x>67 and x<84 and c>9:
                         c-=10
                    if x>84 and x<100 and c<=245:
                         c+=10
                abc(a,b,c,B)
                shua()
